<?php
class BookAction extends CommonAction
{
	public function add()
	{
		if (isset ( $_FILES ['picture'] )) {
			$userfile_name = $_FILES ['picture'] ['name'];
			$userfile_tmp = $_FILES ['picture'] ['tmp_name'];
			$userfile_size = $_FILES ['picture'] ['size'];
			$filename = basename ( $_FILES ['picture'] ['name'] );
			
			$file_ext = substr ( $filename, strrpos ( $filename, '.' ) + 1 );
			$file_ext = strtolower($file_ext);
			if(!in_array($file_ext, array("jpg","jpeg"))) {
				$this->error("错误：文件类型不匹配");
				return;
			}
			
			$large_file_name = uniqid () . "." . $file_ext;
			$large_file_location = C ( 'UPLOAD_FILE_PATH' ) . $large_file_name;
			if (isset ( $_FILES ['picture'] ['name'] )) {
				move_uploaded_file ( $userfile_tmp, $large_file_location );
				$book['picture'] = $large_file_name;
			}
		}
		
		$book['user_id'] = $_SESSION['user']['id'];
		$book['group_id'] = $_POST['group_id'];
		$book['author'] = $_POST['author'];
		$book['title'] = $_POST['title'];
		$book['introduction'] = $_POST['introduction'];
		$book['create_time'] = time();
		$book_dao = M('book');
		$book_dao->add($book);
		$this->success("图书添加成功");
	}
	
	public function addBookPage()
	{
		$group_id = $_GET['group_id'];
		$this->assign("group_id", $group_id);
		$this->display();
	}
	
	public function showAllBooks()
	{
		$book_dao = new BookModel();
		$result = $book_dao->getAll(10);
		$this->assign("books", $result['data']);
		$this->assign("page", $result['page']);
		$this->display();
	}
	
	public function showOneBook()
	{
		$book_id = $_GET['book_id'];
		$book_dao = new BookModel();
		$book = $book_dao->getById($book_id);
		$comment_dao = new CommentModel();
		$comments = $comment_dao->getByBookId($book_id);
		
		$note_dao = new NoteModel();
		$notes = $note_dao->getByBookId($book_id);
		
		$this->assign("book", $book);
		$this->assign("comments", $comments);
		$this->assign("notes", $notes);
		$this->display();
		
	}
	
	public function addLabel() {
		$book_id = $_POST['book_id'];
		$content = $_POST['content'];
		$label_dao = new LabelModel();
		$label_dao->addLabel($book_id, $_SESSION['user']['id'], $content);
		$this->ajaxReturn(null,null,1);
	}
	
}

?>